const phoneRexp = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
import { find } from "../../../api/group";

Page({
    data: {
        tabNav: [
            { show: true, value: 'buyer' },
            { show: false, value: 'supplier' },
            { show: true, value: 'platform' }
        ],
        TabCur: 'buyer',
        isRead: false,
        name: '',
        type: '',
        nature: '',
        phone: '',
        linkman: '',
        group_id: '',
        group_name: '请选择集团',
        sellearAdd: false,
        sellearEdit: false,
        address: '',
        license: '',
        chemical: '',
        wholesale: '',
        other: '',
        id:'',

        legalPersonCode:'',
        legalPerson1:'',
        legalPerson2:'',
        accountLicense:'',
        banknum:'',
        bankname:'',
        legalGrant:''
    },

    onLoad: function (options) {
        this.data.name = options.name
        this.data.type = options.type
        this.data.sellearEdit = options.sellearEdit
        this.data.sellearAdd = options.sellearAdd
        if (this.data.sellearEdit != "undefined") {
            this.data.address = options.address
            this.data.group_id = options.group_id
            this.data.group_name = options.groupName;
            this.data.linkman = options.linkman
            this.data.phone = options.phone
            this.data.nature = options.nature
            this.data.license = options.license;
            this.data.chemical = options.chemical;
            this.data.wholesale = options.wholesale;
            this.data.other = options.other;
            this.data.id = options.id;
            this.data.licenseCode = options.licenseCode;

            this.data.legalPersonCode = options.legalPersonCode;
            this.data.legalPerson1 = options.legalPerson1;
            this.data.legalPerson2 = options.legalPerson2;
            this.data.accountLicense = options.accountLicense;
            this.data.banknum = options.banknum;
            this.data.bankname = options.bankname;
            this.data.legalGrant = options.legalGrant;


            this.setData({
                name: this.data.name,
                type: this.data.type,
                address: this.data.address,
                group_name: this.data.group_name,
                linkman: this.data.linkman,
                phone: this.data.phone,
                nature: this.data.nature,
                licenseCode:this.data.licenseCode
            })
            this.find(options.group_id)
        }
        this.setData({
            name: this.data.name,
            type: this.data.type
        })
    },
    find(id){
        find(id).then(res =>{
            this.data.group_name =  res.data.group.name
            this.setData({
                group_name:this.data.group_name
            })
        })
    },
    phoneChange(e) {
        this.data.phone = e.detail.value
        this.setData({
            phone: this.data.phone
        })
    },
    addressChange(e) {
        this.data.address = e.detail.value
        this.setData({
            address: this.data.address
        })
    },
    linkmanChange(e) {
        this.data.linkman = e.detail.value
        this.setData({
            linkman: this.data.linkman
        })
    },
    updateNature(e) {
        this.data.nature = e.detail.type
        this.setData({
            nature: this.data.nature
        })
    },
    toSearchSelectPage() {
        wx.navigateTo({
            url: '/pages/pagesCommon/common/search-select-page'
        })
    },
    nextStep() {
        if (this.data.nature == "") {
            wx.showToast({
                title: '请选择企业类型',
                icon: 'none'
            })
            return false
        }
        if (this.data.linkman == "") {
            wx.showToast({
                title: '请输入联系人',
                icon: 'none'
            })
            return false
        }
        if (this.data.phone == "") {
            wx.showToast({
                title: '请输入联系电话',
                icon: 'none'
            })
            return false
        }
        if (!this.isPhone(this.data.phone)) {
            wx.showToast({
                icon: 'none',
                title: '手机号格式错误',
            })
            return false
        }
        wx.navigateTo({
            url: '/pages/pagesUser/companyInfo/certUpload?name=' + this.data.name 
                + '&type=' + this.data.type
                + '&nature=' + this.data.nature
                + '&phone=' + this.data.phone 
                + '&linkman=' + this.data.linkman
                + '&group_id=' + this.data.group_id 
                +'&sellearAdd=' + this.data.sellearAdd 
                + '&address=' + this.data.address
                + '&sellearEdit=' + this.data.sellearEdit
                + '&license=' + this.data.license
                + '&chemical=' + this.data.chemical
                + '&wholesale=' + this.data.wholesale
                + '&other=' + this.data.other
                + '&id=' + this.data.id
                + '&licenseCode=' + this.data.licenseCode

                + '&legalPersonCode=' + this.data.legalPersonCode
                + '&legalPerson1=' + this.data.legalPerson1
                + '&legalPerson2=' + this.data.legalPerson2
                + '&accountLicense=' + this.data.accountLicense
                + '&banknum=' + this.data.banknum
                + '&bankname=' + this.data.bankname
                + '&legalGrant=' + this.data.legalGrant
        })
    },
    isPhone(value) {
        if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) {
            return false
        } else {
            return true
        }
    },
    tabSelect(e) {
        this.data.TabCur = e.currentTarget.dataset.id
        this.setData({
            TabCur: this.data.TabCur
        })
    }
});